﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mengsk.ERP.Orders;

namespace Mengsk.ERP.App.Orders.OrderView
{
    class OrderViewModel
    {
        public const string ORDER_STATE_COD_WAIT_SHIP = "STATE_COD_WAIT_SHIP";
        public const string ORDER_STATE_COD_SHIP_OK = "STATE_COD_SHIP_OK";
        public const string ORDER_STATE_COD_SIGN = "STATE_COD_SIGN";
        public const string ORDER_STATE_COD_REFUSE = "STATE_COD_REFUSE";
        public const string ORDER_STATE_COD_SUCESS = "STATE_COD_SUCESS";
        public const string ORDER_STATE_COD_CANCEL = "STATE_COD_CANCEL";

        public DateTime Time { get; set; }

        public Order[] Orders { get; set; }

        public Order[] OrdersWaitShip { get; set; }

        public Order[] OrdersShipOK { get; set; }

        public Order[] OrdersSign { get; set; }

        public Order[] OrdersSucess { get; set; }

        public Order[] OrdersRefusedReturned { get; set; }

        public Order[] OrdersRefusedNotReturned { get; set; }

        public Order[] OrdersNonDelivery { get; set; }

        public Order[] OrdersDuplicateDelivery { get; set; }


        public static OrderViewModel Anlay(Order[] orders)
        {
            if (orders == null)
            {
                throw new ArgumentNullException("orders");
            }

            OrderViewModel vm = new OrderViewModel();
            vm.Orders = (orders.Clone() as Order[]).OrderBy(obj => obj.Shop).ToArray();
            vm.OrdersWaitShip = orders.Where(obj => obj.State.IndexOf(ORDER_STATE_COD_WAIT_SHIP, StringComparison.OrdinalIgnoreCase) >= 0).ToArray();
            vm.OrdersShipOK = orders.Where(obj => obj.State.IndexOf(ORDER_STATE_COD_SHIP_OK, StringComparison.OrdinalIgnoreCase) >= 0 && obj.SellerComment.Contains("返回收到") == false).ToArray();
            vm.OrdersRefusedNotReturned = orders.Where(obj => obj.State.IndexOf(ORDER_STATE_COD_REFUSE, StringComparison.OrdinalIgnoreCase) >= 0 && obj.SellerComment.Contains("返回收到") == false).ToArray();
            vm.OrdersRefusedReturned = orders.Where(obj => obj.SellerComment.Contains("返回收到") == true && obj.SellerComment.Contains("备用物流") == false && obj.SellerComment.Contains("再次发") == false && obj.SellerComment.Contains("准备物流") == false && obj.State != ORDER_STATE_COD_SUCESS && obj.State != ORDER_STATE_COD_SIGN).ToArray();
            vm.OrdersSign = orders.Where(obj => obj.State.IndexOf(ORDER_STATE_COD_SIGN, StringComparison.OrdinalIgnoreCase) >= 0).ToArray();
            vm.OrdersSucess = orders.Where(obj => obj.State.IndexOf(ORDER_STATE_COD_SUCESS, StringComparison.OrdinalIgnoreCase) >= 0).ToArray();
            vm.OrdersNonDelivery = orders.Where(obj => obj.State == ORDER_STATE_COD_SHIP_OK && string.IsNullOrWhiteSpace(obj.DeliveryNumber)).ToArray();

            //分析订单重复
            Dictionary<string, List<Order>> duplicateOrders = new Dictionary<string, List<Order>>();

            foreach (var order in orders)
            {
                if (string.IsNullOrWhiteSpace(order.DeliveryNumber))
                {
                    continue;
                }
                if (duplicateOrders.ContainsKey(order.DeliveryNumber) == false)
                {
                    duplicateOrders[order.DeliveryNumber] = new List<Order>();
                }
                duplicateOrders[order.DeliveryNumber].Add(order);
            }

            List<Order>[] dup = duplicateOrders.Values.Where(obj => obj.Count > 1).ToArray();

            List<Order> dupOrders = new List<Order>();

            foreach (var item in dup)
            {
                dupOrders.AddRange(item);
            }

            vm.OrdersDuplicateDelivery = dupOrders.OrderBy(obj => obj.DeliveryNumber).ToArray();

            return vm;
        }
    }
}
